package com.cat.gridChart;

/**
*   @description https://leetcode.cn/problems/max-area-of-island/description/
*   @author 曲大人的喵
*   @create 2025/8/19 16:38
*   @since JDK17
*/

public class Solution01 {
    int n, m;
    int[][] dire = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    int dfs(int[][] grid, int x, int y) {
        int ans = 0;
        grid[x][y] = 0;
        for (int i = 0; i < 4; i++) {
            int nx = dire[i][0] + x, ny = dire[i][1] + y;
            if (x < 0 || y < 0 || x == n || y == m) {
                continue;
            }
            if (grid[nx][ny] == 1) {
                ans += 1 + dfs(grid, nx, ny);
            }
        }
        return ans;
    }
    public int maxAreaOfIsland(int[][] grid) {
        n = grid.length;
        m = grid[0].length;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (grid[i][j] != 0) {  //
                    ans = Math.max(ans, dfs(grid, i, j));
                }
            }
        }

        return ans;
    }
}
